<?php

namespace app\admin\controller;

use app\admin\model\Log;
use app\admin\model\System as SystemModel;
use app\BaseController;
use app\lib\ZFileDB;
use app\Request;

class System extends BaseController
{

    public function getLog(Request $request)
    {
        $cid = input('cid', 0, 'intval');
        $page = input('page', 1, 'intval');
        $perpage = 8;
        $type = $request->get('type');
        $content = $request->get('content');
        if (!empty($content)) {
            IF ($type == 1) {
                $where[] = [
                    'fksj', '=', $content
                ];
            } else {
                $where[] = [
                    'sksj', '=', $content
                ];
            }
            $result = Log::order('id desc')->where($where)->page($page, $perpage)->select();
            $total = Log::where($where)->count();
        } ELSE {
            $result = Log::order('id desc')->page($page, $perpage)->select();
            $total = Log::count();
        }

        foreach ($result as &$item) {
            if (!empty($item['files'])) {
                $arr = [];
                foreach ($item['files'] as $item2) {
                    $arr[] = $request->domain() . $item2;
                }
                $item['files_url'] = $arr;
            } else {
                $item['files_url'] = [];
            }
        }
        $data = [
            'total' => $total,
            'current' => $page,
            'perpage' => $perpage,
            'data' =>  $result
        ];
        return show(config('status.success'), '获取列表', $data);
    }

    public function createLog(Request $request)
    {
        Log::create([
            'ghgs' => $request->post('ghgs'),
            'cght' => $request->post('cght'),
            'cgdj' => $request->post('cgdj'),
            'cgsl' => $request->post('cgsl'),
            'cgjk' => $request->post('cgjk'),
            'fksj' => $request->post('fksj'),
            'shdw' => $request->post('shdw'),
            'xsht' => $request->post('xsht'),
            'xsdj' => $request->post('xsdj'),
            'xssl' => $request->post('xssl'),
            'xsjk' => $request->post('xsjk'),
            'sksj' => $request->post('sksj'),
            'zdyh' => $request->post('zdyh'),
            'files' => ($request->post('files')),
        ]);

        return show(config('status.success'), '操作成功');
    }

    /* 列表 */
    public function index()
    {
        if (!$this->request->auth) return show(config('status.auth'), 'Authorization Failed');
        $data = SystemModel::order('id asc')->select();
        return show(config('status.success'), '获取列表', $data);
    }

    /* 添加 */
    public function add()
    {
        if (!$this->request->auth) return show(config('status.auth'), 'Authorization Failed');
        $name = input('name', '');
        $tel = input('tel', '');
        $map = input('map', '');
        $status = input('status', 0, 'intval');

        $result = SystemModel::create([
            'name' => $name,
            'tel' => $tel,
            'map' => $map,
            'status' => $status
        ]);
        if ($result) {
            return show(config('status.success'), '添加成功');
        } else {
            return show(config('status.error_insert'), '添加失败');
        }
    }

    /* 获取 */
    public function get()
    {
        if (!$this->request->auth) return show(config('status.auth'), 'Authorization Failed');
        $id = input('id', 0, 'intval');
        $cid = input('cid', 0, 'intval');

        if ($cid) {
            $data = SystemModel::where('id', $cid)->find();
        } else {
            $data = SystemModel::find($id);
        }

        if ($data) {
            return show(config('status.success'), '查询成功', $data);
        } else {
            return show(config('status.error_find'), '查询失败');
        }
    }

    /* 修改 */
    public function modifySystem()
    {
        if (!$this->request->auth) return show(config('status.auth'), 'Authorization Failed');
        $id = input('id', 0, 'intval');
        $cid = input('cid', 0, 'intval');
        $name = input('name', '');
        $tel = input('tel', '');
        $desc = input('desc', '');
        $map = input('map', '');
        $status = input('status', 0, 'intval');

        if ($cid) {
            $result = SystemModel::update([
                'name' => $name,
                'tel' => $tel,
                'desc' => $desc
            ], ['id' => $cid]);
        } else {
            $result = SystemModel::update([
                'id' => $id,
                'name' => $name,
                'tel' => $tel,
                'map' => $map,
                'status' => $status
            ]);
        }

        if ($result) {
            return show(config('status.success'), '修改成功');
        } else {
            return show(config('status.error_update'), '修改失败');
        }
    }

    /* 修改 */
    public function modifyWechat()
    {
        if (!$this->request->auth) return show(config('status.auth'), 'Authorization Failed');
        $cid = input('cid', 0, 'intval');
        $appid = input('appid', '');
        $appsecret = input('appsecret', '');

        $result = SystemModel::update([
            'appid' => $appid,
            'appsecret' => $appsecret
        ], ['id' => $cid]);
        if ($result) {
            return show(config('status.success'), '修改成功');
        } else {
            return show(config('status.error_update'), '修改失败');
        }
    }
}
